package com.chaoxuan.uebt.fragment;

import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chaoxuan.uebt.R;
import com.chaoxuan.uebt.activity.DealAddActivity;
import com.chaoxuan.uebt.activity.DealModifyActivity;
import com.chaoxuan.uebt.activity.LoginActivity;
import com.chaoxuan.uebt.activity.MemberActivity;
import com.chaoxuan.uebt.activity.OrderActivity;
import com.chaoxuan.uebt.activity.UserModifyPwdActivity;
import com.chaoxuan.uebt.activity.WalletActivity;
import com.chaoxuan.uebt.activity.WalletAddActivity;
import com.chaoxuan.uebt.activity.WalletModifyActivity;
import com.chaoxuan.uebt.model.User;
import com.chaoxuan.uebt.model.Wallet;
import com.chaoxuan.uebt.utils.BaseSubscriber;
import com.chaoxuan.uebt.utils.Const;
import com.chaoxuan.uebt.utils.ICallBack;
import com.chaoxuan.uebt.utils.QrcodeDialog;
import com.chaoxuan.uebt.utils.RetrofitUtils;
import com.chaoxuan.uebt.utils.CacheUtils;
import com.chaoxuan.uebt.utils.ToastUtils;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


/**
 * Created by Administrator on 2017/3/13.
 */
public class UserFragment extends Fragment implements View.OnClickListener {
    TextView tvUser,tvInvitecode,tvWalletAddress,tvPoints,tvExit;
    TextView tvAllIncome,tvWallet,tvDigIncome;
    ImageView imgTag;
    ICallBack callBack;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_user, container, false);
        tvUser=(TextView)rootView.findViewById(R.id.btn_user);
        tvPoints=(TextView)rootView.findViewById(R.id.tv_points);

        tvWallet=(TextView)rootView.findViewById(R.id.tv_wallet);
        tvAllIncome=(TextView)rootView.findViewById(R.id.tv_all_income);
        tvDigIncome=(TextView)rootView.findViewById(R.id.tv_dig_income);
        imgTag=(ImageView)rootView.findViewById(R.id.img_tag);
        tvInvitecode=(TextView)rootView.findViewById(R.id.tv_invitecode);
        tvWalletAddress=(TextView)rootView.findViewById(R.id.tv_wallet_address);

        rootView.findViewById(R.id.btn_qrcode).setOnClickListener(this);
        rootView.findViewById(R.id.btn_wallet_address).setOnClickListener(this);
        rootView.findViewById(R.id.btn_order).setOnClickListener(this);
        rootView.findViewById(R.id.btn_wallet).setOnClickListener(this);
        rootView.findViewById(R.id.btn_member).setOnClickListener(this);
        rootView.findViewById(R.id.btn_pwd).setOnClickListener(this);
        rootView.findViewById(R.id.btn_copy).setOnClickListener(this);
        rootView.findViewById(R.id.btn_deal_pwd).setOnClickListener(this);
        tvExit=(TextView) rootView.findViewById(R.id.btn_exit);
        tvExit.setOnClickListener(this);
        tvUser.setOnClickListener(this);
        show();
        load();
        return rootView;
    }

    @Override
    public void onClick(View view) {
        User user=CacheUtils.getInstance(getActivity()).getUser();
        if(user==null) {
            if(view.getId()==R.id.btn_user){
                startActivity(new Intent(getActivity(), LoginActivity.class));
            }
            return;
        }
        switch (view.getId()) {
            case R.id.btn_qrcode:
                String href="http://www.chaoxuan.io/portal/public/register?invitecode="+tvInvitecode.getText().toString();
                Log.d("--",href);
                new QrcodeDialog(getContext(),callBack,R.style.dialog,href).show();
                break;
            case R.id.btn_order:
                startActivity(new Intent(getActivity(),OrderActivity.class));
                break;
            case R.id.btn_wallet_address:
                if(TextUtils.isEmpty(tvWalletAddress.getText().toString())) {
                    startActivity(new Intent(getActivity(), WalletAddActivity.class));
                }else{
                    Intent i=new Intent(getActivity(), WalletModifyActivity.class);
                    i.putExtra("address",tvWalletAddress.getText().toString());
                    startActivity(i);
                }
                break;
            case R.id.btn_member:
                startActivity(new Intent(getActivity(),MemberActivity.class));
                break;
            case R.id.btn_wallet:
                startActivity(new Intent(getActivity(),WalletActivity.class));
                break;
            case R.id.btn_copy:
                String code=tvInvitecode.getText().toString();
                String text="加入超选获取区块链积分收益，邀请码 "+code+"\nhttp://www.chaoxuan.io/portal/public/register?invitecode="+code;
                ClipboardManager cm = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
                cm.setText(text);
                ToastUtils.showCustomToast("复制成功");
                break;
            case R.id.btn_pwd:
                startActivity(new Intent(getActivity(),UserModifyPwdActivity.class));
                break;
            case R.id.btn_deal_pwd:
                if(TextUtils.isEmpty(user.getDeal_pass())) {
                    startActivity(new Intent(getActivity(), DealAddActivity.class));
                }else{
                    startActivity(new Intent(getActivity(), DealModifyActivity.class));
                }
                break;
            case R.id.btn_exit:
                CacheUtils.getInstance(getContext()).remove();
                show();
                callBack.logout();
                break;
        }
    }


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (getUserVisibleHint()) {

        } else {

        }
    }

    @Override
    public void onResume() {
        super.onResume();
        load();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {

        } else {
            show();
        }
    }


    private void show() {
        User user= CacheUtils.getInstance(getContext()).getUser();
        Log.d("--------------",""+(user==null));
        if(user==null){
            tvUser.setText("登录账号");
            tvInvitecode.setText("");
            tvWalletAddress.setText("");

            tvWallet.setText("0.00");
            tvAllIncome.setText("0.00");
            tvDigIncome.setText("0.00");
            imgTag.setVisibility(View.GONE);
            tvExit.setVisibility(View.GONE);
            return;
        }
        if(user.getU_tag()==0){
            imgTag.setImageResource(R.mipmap.tag0);
        }else if(user.getU_tag()==1){
            imgTag.setImageResource(R.mipmap.tag1);
        }else if(user.getU_tag()==2){
            imgTag.setImageResource(R.mipmap.tag2);
        }
        imgTag.setVisibility(View.VISIBLE);
        tvUser.setText(user.getUser_login());
        tvInvitecode.setText(user.getInvitecode());
        tvWalletAddress.setText(user.getWallet_url());
        tvPoints.setText("邀请成功+"+user.getPoints()+"分");
        tvExit.setVisibility(View.VISIBLE);

    }

    private void load() {
        final User user= CacheUtils.getInstance(getContext()).getUser();
        if(user==null)return;
        RetrofitUtils.getAPIService().userIndex(user.getToken(),Const.DEVICE_TYPE)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new BaseSubscriber<User>() {
                @Override
                public void onSuccess(User data) {
                    money(user);
                    data.setToken(user.getToken());
                    data.setId(user.getId());
                    CacheUtils.getInstance(getContext()).save(data);
                    show();
                }
            });
    }
    private void money(User user){
        RetrofitUtils.getAPIService().wallet(user.getToken(),Const.DEVICE_TYPE)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new BaseSubscriber<Wallet>() {
                @Override
                public void onSuccess(Wallet data) {
                    tvWallet.setText(data.getWallet());
                    tvAllIncome.setText(data.getAll_income());
                    tvDigIncome.setText(data.getDig_income());
                }
            });
    }

    public void setCallBack(ICallBack callBack){
        this.callBack=callBack;
    }

}
